package com.kitty.game.drop.newservice;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.ServerService;
import com.kitty.game.base.service.BagService;
import com.kitty.game.enter.TitleInfo;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.equip.service.FabaoService;
import com.kitty.game.item.service.PointItemService;
import com.kitty.game.map.service.MapService;
import com.kitty.game.pet.PetDataPool;
import com.kitty.game.pet.bean.PetObject;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.pet.service.PetService;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.utils.Const;
import com.kitty.common.model.Reason;
import com.kitty.mina.message.MessagePusher;
import com.kitty.common.utils.DecodeSendMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.lang.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 新的掉落逻辑
 */
@Service
@Slf4j
public class NewDropService {
    @Autowired
    EquipService equipService;
    @Autowired
    PetService petService;
    @Autowired
    RoleService roleService;
    @Autowired
    BagService bagService;
    /**添加道具直接到背包*/
    public void addRewardItem(Role role, String reward,String notice) {
        HashMap<String, Object> buildRewardMessageMap =  new HashMap<>();
        String sendYaoyan = "";
        int newType = -1;
        try{
            buildRewardMessageMap = DecodeSendMessage.buildSendRewardMessage(reward);
            newType = Integer.parseInt(buildRewardMessageMap.get("type").toString());
        }catch(Exception e){
            newType = -1;
        }
        if(newType == Const.rewardWupin){
            //如果是物品的话。取个数，取是否叠加
            short pos = bagService.getPos(role, false);
            if (pos <= 0) {
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("背包已满"));
                return;
            }
            String itemName  = buildRewardMessageMap.get("itemName").toString();
            String itemCount  = buildRewardMessageMap.get("itemCount").toString();
            equipService.addMall(itemName, true, role, Integer.parseInt(itemCount));


            //这里发送谣言会不会有什么问题
            sendYaoyan = getNumor(role,notice,itemName,Integer.parseInt(itemCount));
        }else if(newType == Const.rewardChongwu || newType == Const.rewardJingguai){
            String petName  = buildRewardMessageMap.get("petName").toString();
            String petType = buildRewardMessageMap.get("petType").toString();
            if (SpringUtils.getPetService().isEquippedFull(role)) {
                MessagePusher.pushMessage(role, new RespMsg("当前宠物包裹已满，请整理后再来吧。"));
                return;
            }
            PetObject petObject = PetDataPool.getPetObject(petName);
            Pet pet = petService.addPet(petObject, role, false);
            petService.loadPet(role, pet);
            RespNotifyMiscEx respNotifyMiscEx = new RespNotifyMiscEx();
            respNotifyMiscEx.setMsg("你获得了一只#R" + petObject.getName() + "#n。");
            respNotifyMiscEx.setTime(new Long(System.currentTimeMillis() / 1000).intValue());
            MessagePusher.pushMessage(role, respNotifyMiscEx);

            //这里发送谣言会不会有什么问题
            sendYaoyan = getNumor(role,notice,petName,0);
        }else if (newType == Const.rewardFabao){
            String name  = buildRewardMessageMap.get("fabaoName").toString();
            int level = Integer.parseInt(buildRewardMessageMap.get("fabaoLevel").toString());
            int xiangxing = Integer.parseInt(buildRewardMessageMap.get("fabaoXiangxing").toString());
            SpringUtils.getBean(FabaoService.class).getArtifact(role, name, xiangxing, level);
            RespNotifyMiscEx respNotifyMiscEx = new RespNotifyMiscEx();
            respNotifyMiscEx.setMsg("你获得了一个#R" + name + "#n。");
            respNotifyMiscEx.setTime(new Long(System.currentTimeMillis() / 1000).intValue());
            MessagePusher.pushMessage(role, respNotifyMiscEx);

            //这里发送谣言会不会有什么问题
            sendYaoyan = getNumor(role,notice,name,0);
        }else if(newType == Const.rewardJifen){
            int value = Integer.parseInt(buildRewardMessageMap.get("count").toString());
            SpringUtils.getRoleService().addRechargeScore(role, value);

            sendYaoyan = getNumor(role,notice,"积分",value);
        }else if(newType == Const.rewardqimenJifen){
            int value = Integer.parseInt(buildRewardMessageMap.get("count").toString());
            SpringUtils.getRoleService().addmijingjifen(role, value);

            sendYaoyan = getNumor(role,notice,"奇门积分",value);
        }else if(newType == Const.rewardhaoxinzhi){
            int value = Integer.parseInt(buildRewardMessageMap.get("count").toString());
            SpringUtils.getRoleService().addhaoxianzhi(role, value);

            sendYaoyan = getNumor(role,notice,"好心值",value);
        }else if(newType == Const.rewardGuiqi){
            int value = Integer.parseInt(buildRewardMessageMap.get("count").toString());
            SpringUtils.getRoleService().addGuiQi(role,value,true);

            sendYaoyan = getNumor(role,notice,"鬼气",value);
        }else if(newType == Const.rewardWuhun){
            int value = Integer.parseInt(buildRewardMessageMap.get("count").toString());
            SpringUtils.getRoleService().addWuling(role,value,false);

            sendYaoyan = getNumor(role,notice,"武灵值",value);
        }else if(newType == Const.rewardYinde){
            int value = Integer.parseInt(buildRewardMessageMap.get("count").toString());
            SpringUtils.getRoleService().addYinde(role,value,true);

            sendYaoyan = getNumor(role,notice,"阴德",value);
        }else if(newType == Const.rewardJinyuanbao){
            int value = Integer.parseInt(buildRewardMessageMap.get("count").toString());
            SpringUtils.getRoleService().addGold(role, value, Reason.MAIL_ITEM);
            roleService.updateRoleGoldAndSiver(role);

            sendYaoyan = getNumor(role,notice,"金元宝",value);
        }else if(newType == Const.rewardYinyuanbao){
            int value = Integer.parseInt(buildRewardMessageMap.get("count").toString());
            roleService.addSiver(role, value, Reason.FETCH_MAIL);

            sendYaoyan = getNumor(role,notice,"银元宝",value);
        }else if(newType == Const.rewardLuoshu){
            int value = Integer.parseInt(buildRewardMessageMap.get("count").toString());
            roleService.addluoshuExp(role, value);

            sendYaoyan = getNumor(role,notice,"洛书经验",value);
        }else if(newType == Const.rewardJingYan){
            int value = Integer.parseInt(buildRewardMessageMap.get("count").toString());
            roleService.addExp(role, value, role.getLevel(), 0);
            sendYaoyan = getNumor(role,notice,"经验",value);
        }else if(newType == Const.rewardTao){
            int value = Integer.parseInt(buildRewardMessageMap.get("count").toString());
            roleService.addPot(role, ThreadLocalRandom.current().nextInt(1, 4) * role.getLevel() * value / 5);
            sendYaoyan = getNumor(role,notice,"道行",value);
        }else if(newType == Const.rewardShoushi){
            String name = buildRewardMessageMap.get("name").toString();
            String value = buildRewardMessageMap.get("value").toString();
            SpringUtils.getEquipService().getJewelryNew(role, name, 1, value, false);
            sendYaoyan = getNumor(role,notice,name,0);
        }
        if(StringUtils.isNotEmpty(sendYaoyan)){
            SpringUtils.getChatService().sendNumor(sendYaoyan, Const.BRODCAST_MSG_TYPE_ROLE);
        }
    }
    /**
     * 获得掉落谣言内容
     */
    public String getNumor(Role role, String numor, String dropItemName,int count) {
        if (StringUtils.isNotEmpty(numor)) {
            /**替换角色名字、道具名字等*/
            String monsterName = role.getTempCache("drop_monster_name", "");
            numor = numor
                    .replaceAll("#角色名字#", role.getName())
                    .replaceAll("#道具名字#", dropItemName)
                    .replaceAll("#帮派名字#", role.getPartyName())
                    .replaceAll("#区名字#", SpringUtils.getBean(ServerService.class).getServer().getSonName())
                    .replaceAll("#地图名字#", SpringUtils.getBean(MapService.class).getMap(role.getPos().getMapId()).getName())
                    .replaceAll("#怪物名字#", monsterName)
                    .replaceAll("#道具数量#", count+"")
                    ;
            return numor;
        }

        return null;
    }
    public void sendRewardMail(Role role,String reward){
        DecodeSendMessage.buildSendRewardMessage(reward);
    }
    public void addRewardTask(Role role,String reward,String notice){
        HashMap<String, Object> buildRewardMessageMap =  new HashMap<>();
        String sendYaoyan = "";
        try{
            buildRewardMessageMap = DecodeSendMessage.buildSendRewardMessage(reward);
        }catch(Exception e){
        }
        String mailData = buildRewardMessageMap.getOrDefault("data","0").toString();
        int type = Integer.parseInt(buildRewardMessageMap.getOrDefault("type","0").toString());
        String data = mailData.replaceAll("#I","");
        String showType = data.split("\\|")[0];
        String showName = data.split("\\|")[1].split("#r")[0];
        int number = Integer.parseInt(data.split("\\|")[1].split("#r")[1]);

        if(type == Const.rewardWupin){
            short pos = bagService.getPos(role, false);
            if (pos <= 0) {
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("背包已满"));
                return;
            }
            equipService.addMall(showName, true, role, number);
            //这里发送谣言会不会有什么问题
            sendYaoyan = getNumor(role,notice,showName,number);
        }else if(type == Const.rewardJifen){
            roleService.addRechargeScore(role, number);
            sendYaoyan = getNumor(role,notice,showType,number);
        }else if(type == Const.rewardqimenJifen){
            roleService.addmijingjifen(role, number);
            sendYaoyan = getNumor(role,notice,showType,number);
        }else if(type == Const.rewardhaoxinzhi){
            roleService.addhaoxianzhi(role, number);
            sendYaoyan = getNumor(role,notice,showType,number);
        }else if(type == Const.rewardTao){
            roleService.addTao(role, number);
            sendYaoyan = getNumor(role,notice,showType,number);
        }else if(type == Const.rewardChongwu){
            String petName = buildRewardMessageMap.getOrDefault("petName","松鼠").toString();
            if (SpringUtils.getPetService().isEquippedFull(role,petName)) {
                MessagePusher.pushMessage(role, new RespMsg("当前宠物包裹已满，请整理后再来吧。"));
                return;
            }
            if (!Strings.isEmpty(petName)) {
                PetObject petObject = PetDataPool.getPetObject(petName);
                Pet pet = petService.addPet(petObject, role, false);
                petService.loadPet(role, pet);
                RespNotifyMiscEx respNotifyMiscEx = new RespNotifyMiscEx();
                respNotifyMiscEx.setMsg("你获得了一只#R" + petObject.getName() + "#n。");
                respNotifyMiscEx.setTime(new Long(System.currentTimeMillis() / 1000).intValue());
                MessagePusher.pushMessage(role, respNotifyMiscEx);
            }
            sendYaoyan = getNumor(role,notice,petName,0);
        }else if(type == Const.rewardShoushi){
            String name = showName.split("\\(")[0];
            if("低级随机".equals(name)){
                int[] levels = {20, 35, 50,60,70};
                int index = ThreadLocalRandom.current().nextInt(levels.length);
                RoleEquip roleEquip = equipService.getRandomJewelry(levels[index], role);
                if(roleEquip== null){
                    return;
                }
                equipService.refreshRoleEquip(role,roleEquip);
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("你获得了一件#R" + roleEquip.getName() + "#n。"));
            }else{
                SpringUtils.getEquipService().getJewelryNew(role, name, 1, showName, false);
                sendYaoyan = getNumor(role,notice,showName,number);
            }
        }else if(type ==Const.rewardFabao){
            int polar = Integer.parseInt(buildRewardMessageMap.getOrDefault("polar","1").toString());
            SpringUtils.getBean(FabaoService.class).getArtifact(role, showName, polar, number);
            sendYaoyan = getNumor(role,notice,showName,0);
        }else if(type == Const.rewardJinyuanbao){
            roleService.addGold(role, number, Reason.MAIL_ITEM);
            sendYaoyan = getNumor(role,notice,showType,number);
        }else if(type == Const.rewardYinyuanbao){
            roleService.addSiver(role, number, Reason.FETCH_MAIL);
            sendYaoyan = getNumor(role,notice,showType,number);
        }else if(type == Const.mailMoney){
            roleService.addMoney(role, number, Reason.FETCH_MAIL);
            sendYaoyan = getNumor(role,notice,showType,number);
        }else if(type ==  Const.rewardGuiqi){
            roleService.addGuiQi(role,number,true);
            sendYaoyan = getNumor(role,notice,showType,number);
        }else if(type == Const.rewardYinde){
            roleService.addYinde(role,number,true);
            sendYaoyan = getNumor(role,notice,showType,number);
        }else if(type == Const.rewardWuhun){
            roleService.addWuling(role,number,false);
            sendYaoyan = getNumor(role,notice,showType,number);
        }else if(type == Const.rewardLuoshu){
            roleService.addluoshuExp(role, number);
            sendYaoyan = getNumor(role,notice,showType,number);
        }else if("魂器".equals(type)){

        }else if("魂窍".equals(type)){

        }else if(type == Const.rewardJingYan){
            roleService.addExp(role, number, role.getLevel(), 0);
            sendYaoyan = getNumor(role,notice,"经验",number);
        }else if(type == Const.rewardTao){
            roleService.addPot(role, ThreadLocalRandom.current().nextInt(1, 4) * role.getLevel() * number / 5);
            sendYaoyan = getNumor(role,notice,"道行",number);
        }else if("mailEquip".equals(type)){

        }else if (type == Const.mailPointItem) {
            if (showName.equals(Const.CHONG_FEN_SAN_ITEM_NAME)) {
                SpringUtils.getBean(PointItemService.class).addChongFengSan(role, number);
            } else if (showName.equals(Const.ZI_QI_HONG_MENG_ITEM_NAME)) {
                SpringUtils.getBean(PointItemService.class).addZiQiHongMeng(role, number);
            }
        }else if (type == Const.rewardChenghao) {
            String titleName = showName;
            SpringUtils.getRoleService().addTitle(role, new TitleInfo(titleName, titleName));
        }
        if(StringUtils.isNotEmpty(sendYaoyan)){
            SpringUtils.getChatService().sendNumor(sendYaoyan, Const.BRODCAST_MSG_TYPE_ROLE);
        }
    }

}
